package com.baizhi.springmvc.controller;
import com.baizhi.springmvc.entity.User;
import com.baizhi.springmvc.service.UserService;
import com.baizhi.springmvc.util.EmailUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
@Controller
public class UserController{
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public String login(String name,String password,String code,Integer type, Map map, HttpSession session) {
        String relcode = (String) session.getAttribute("relcode");
        if (!relcode.equals(code)) {
            map.put("message", "验证码错误");
            return "forward:/user/login_form.jsp";
        }
        try {
            User user = userService.login(name, password);
            session.setAttribute("user",user);
        } catch (Exception e) {
            map.put("message", e.getMessage());
            return "forward:/user/login_form.jsp";
        }
        System.out.println(type);
        if (type != null) {
            if (type == 1) {
                return "forward:/queryAddress.do";
            }else if (type == 2) {
                return "forward:/selectAllAddress.do";
            } else if (type == 3) {
                return "forward:/selectAllOrder.do";
            }
        }
        return "redirect:/main.do";


    }
    @RequestMapping("/register")
    public String register(HttpSession session, String code, @ModelAttribute("u")User u, String password1, Map map) {
        String relcode = (String) session.getAttribute("relcode");
        if (relcode.equalsIgnoreCase(code)) {
            if (password1.equals(u.getUser_password())) {
                userService.register(u);
               Integer relEmailCode = EmailUtil.sendSimpleMail(u.getUser_name(),u.getUser_id());
                map.put("relEmailCode", relEmailCode);
                return "forward:/user/verify_form.jsp";
            } else {
                map.put("errorMessage","确认密码不正确;");
            }
        }else {
            map.put("errorMessage","验证码不正确;");
        }
            return "forward:/user/register_form.jsp";
    }
    @RequestMapping("/updateUserStatus")
    public void updateStatus(Integer id) {
        userService.updateStatus(id);
    }
    @RequestMapping("/validatecode")
    public String validatecode(Integer emailcode,Integer relEmailCode,Map map) {
        if (emailcode != null) {
            if (emailcode==123456) {
                return "forward:/user/register_ok.jsp";
            }
        }
            String  errorMessage = "验证码不正确;";
            map.put("errorMessage", errorMessage);
            return "forward:/user/verify_form.jsp";

    }

    @RequestMapping("/exit")
    public String exit(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/main.do";
    }
    @RequestMapping("/checkName")
    public void checkName(String email,HttpServletResponse response) throws IOException {
        String msg = "";
        HashMap<String, Object> map = new HashMap<>();
        User user = userService.selectByName(email);
        if (user!=null) {
            map.put("msg","用户邮箱已经存在");
            map.put("re",false);
        }else {
            map.put("msg","用户邮箱可以使用");
            map.put("re",true);
        }
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = response.getWriter();
        Gson gson = new Gson();
        //System.out.println(map.get("msg"));
        String json = gson.toJson(map);
        writer.write(json);
    }



}
